﻿/**********************************弘石项目*******************************************
* 功能描述:系统功能显示菜单
* 作者:石锦涛
* 时间:2021-12-16
* 
**********************************弘石项目*******************************************/
using Dapper;
using HongStone.Domain.AttendanceManagement;
using HongStone.Domain.OperationsManagement;
using HongStone.Domain.SystemSetupModel.AuthorityManagement;
using HongStone.Domain.SystemSetupModel.FrequentlyUsedDataModel;
using HongStone.Domain.SystemSetupModel.InstitutionSettingModel;
using HongStone.Dto;
using HongStone.Ince.IClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HongStone.Repository
{
    public class DisplayOptionsDal
    {
        private readonly HongStoneContext _hongStoneContext;
        private readonly ILogger<DisplayOptionsDal> _logger;

        private readonly IConfiguration _configuration;
        public DisplayOptionsDal(HongStoneContext hongStoneContext, IConfiguration configuration, ILogger<DisplayOptionsDal> logger)
        {
            _hongStoneContext = hongStoneContext;
            _configuration = configuration;
            _logger = logger;
            _logger.LogDebug(1, "NLog injected into HomeController");
        }


        /// <summary>
        /// 系统功能显示
        /// </summary>
        /// <returns></returns>
        public List<FunctionDto> ShowFunction()
        {
            try
            {
                using (var conn = new SqlConnection(_configuration.GetConnectionString("MSSQL")))
                {
                    var list = conn.Query<Function>("select * from [Function]").ToList();
                    return FunctionDG(list);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<FunctionDto> FunctionDG(List<Function> functions,string moduleGrade="")
        {
            return functions.Where(p => p.ModuleGrade == moduleGrade).Select(p => new FunctionDto
            {
                ModuleId = p.ModuleId,
                ModuleName = p.ModuleName,
                ModuleGrade = p.ModuleGrade,
                Level = p.Level,
                Url = p.Url,
                OrderIndex = p.OrderIndex,
                SEstablishTime = p.SEstablishTime,
                ChildMenus = FunctionDG(functions, p.ModuleId).Count == 0 ? null : FunctionDG(functions, p.ModuleId)
            }).ToList();
        }


    }
}
